Fix: Support Sequential models in Grad-CAM example#2244
Fix: Support Sequential models in Grad-CAM example#2244jaytiwarihub wants to merge 4 commits intokeras-team:masterfrom
Conversation
Summary of ChangesHello @jaytiwarihub, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical compatibility issue within the Grad-CAM example, ensuring that the visualization technique can be applied consistently across different Keras model architectures. By adjusting how the input tensor is accessed, the example now robustly supports both Functional API and simpler Sequential models, broadening its utility and preventing common errors for users. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request correctly addresses a compatibility issue with Keras Sequential models in the Grad-CAM example by switching from model.inputs to model.input. This change enhances the robustness of the example, making it work for both Functional and Sequential models. I've added one minor comment regarding code formatting.
examples/vision/grad_cam.py
Outdated
| grad_model = keras.models.Model( | ||
| model.input, [model.get_layer(last_conv_layer_name).output, model.output] | ||
| ) |
There was a problem hiding this comment.
The core change from model.inputs to model.input is correct and improves compatibility. However, there's a minor indentation issue introduced. The grad_model variable is indented with 3 spaces instead of the standard 4, which violates PEP 8 formatting guidelines. I've suggested a fix to correct the indentation.
| grad_model = keras.models.Model( | |
| model.input, [model.get_layer(last_conv_layer_name).output, model.output] | |
| ) | |
| grad_model = keras.models.Model( | |
| model.input, [model.get_layer(last_conv_layer_name).output, model.output] | |
| ) |
|
A corresponding .md file will need to be generated. Instructions here - https://github.com/keras-team/keras-io?tab=readme-ov-file#adding-a-new-code-example |
|
@divyashreepathihalli Thanks for the review! I have updated the grad_cam.md file as requested. I also included a small fix in scripts/tutobooks.py. The autogen script was crashing on Windows due to encoding errors (UnicodeDecodeError), so I added explicit encoding='utf-8' handling to fix it. Everything is syncing correctly now. |
sachinprasadhs
left a comment
There was a problem hiding this comment.
There is no need for this change, since Xception has a single input, all the models created by keras.applications usually returns single input which is cleaner and works well here.
|
@sachinprasadhs My intention with this change was to make make_gradcam_heatmap more robust. Many users copy-paste this function to debug their own custom models, and if they use a Sequential model, the current implementation fails because of how inputs are handled. |
|
This PR is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you. |
|
This PR is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you. |
This PR fixes a compatibility issue in examples/vision/grad_cam.py where the make_gradcam_heatmap function would fail for Sequential models.
Currently, the function uses model.inputs to construct the gradient model. While this works for Functional API models (like Xception in the example), it often raises errors or behaves unexpectedly with simple Sequential models that haven't explicitly built their graph with an InputLayer.
Changes:
Changed model.inputs to model.input in make_gradcam_heatmap.
This ensures the function works robustly for both Sequential and Functional models, as model.input is the standard way to access the single input tensor for image classifiers.